package com.ueboot.weishop.service.order.impl;

import com.google.common.collect.Lists;
import com.ueboot.weishop.entity.order.OrderIssueEntity;
import com.ueboot.weishop.repository.order.OrderIssueNewRepository;
import com.ueboot.weishop.service.order.OrderIssueNewService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrderIssueNewServiceImpl implements OrderIssueNewService {

	@Autowired
	private OrderIssueNewRepository orderIssueNewRepository;

	@Override
	public Page<OrderIssueEntity> pagingSearch(Pageable pageable, OrderIssueEntity model, Date lastSendDmsTimeStart, Date lastSendDmsTimeEnd) {
		Assert.notNull(model);
		Page<OrderIssueEntity> pageDataOrder = orderIssueNewRepository.findAll((root, query, criteriaBuilder) -> {
			List<Predicate> predicateList = this.commonPredicates(root, criteriaBuilder, model, lastSendDmsTimeStart, lastSendDmsTimeEnd);
			Predicate[] predicates = new Predicate[predicateList.size()];
			return criteriaBuilder.and(predicateList.toArray(predicates));
		}, pageable);
		return pageDataOrder;
	}

	@Override
	public List<OrderIssueEntity> findSearch(OrderIssueEntity model, Date lastSendDmsTimeStart, Date lastSendDmsTimeEnd) {
		Assert.notNull(model);
		List<OrderIssueEntity> orderEntityList = orderIssueNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
			List<Predicate> predicateList = this.commonPredicates(root, criteriaBuilder, model, lastSendDmsTimeStart, lastSendDmsTimeEnd);
			Predicate[] predicates = new Predicate[predicateList.size()];
			return criteriaBuilder.and(predicateList.toArray(predicates));
		});
		return orderEntityList;
	}

	private List<Predicate> commonPredicates(Root<OrderIssueEntity> root, CriteriaBuilder criteriaBuilder, OrderIssueEntity model, Date lastSendDmsTimeStart, Date lastSendDmsTimeEnd) {
		List<Predicate> predicateList = Lists.newLinkedList();

		if (StringUtils.isNotEmpty(model.getSendDmsStatus())) {
			predicateList.add(criteriaBuilder.equal(root.get("sendDmsStatus").as(String.class), model.getSendDmsStatus()));
		}


		if (StringUtils.isNotEmpty(model.getSendSmsStatus())) {
			predicateList.add(criteriaBuilder.equal(root.get("sendSmsStatus").as(String.class), model.getSendSmsStatus()));
		}


		if (StringUtils.isNotEmpty(model.getCampaignCode())) {
			predicateList.add(criteriaBuilder.equal(root.get("campaignCode").as(String.class), model.getCampaignCode()));
		}


		if (StringUtils.isNotEmpty(model.getName())) {
			predicateList.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + model.getName() + "%"));
		}


		if (StringUtils.isNotEmpty(model.getBatchNo())) {
			predicateList.add(criteriaBuilder.equal(root.get("batchNo").as(String.class), model.getBatchNo()));
		}

		if (StringUtils.isNotEmpty(model.getSeqNo())) {
			predicateList.add(criteriaBuilder.equal(root.get("seqNo").as(String.class), model.getSeqNo()));
		}

		if (lastSendDmsTimeStart != null) {
			predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("lastSendDmsTime").as(Date.class), lastSendDmsTimeStart));
		}

		if (lastSendDmsTimeEnd != null) {
			predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("lastSendDmsTime").as(Date.class), lastSendDmsTimeEnd));
		}
		return predicateList;
	}
}
